1. Core Engine Lab
After completing this lab, you should understand the following Apache Camel key concepts:
Route
Processor
CamelContext
DSL
Message
Exchange
You should be familiar with JBoss Developer Studio and the Fuse Camel Editor.
The lab exercises and solutions are available in the following zip archives:
1.1. Explore a Project
The goal of this exercise is to familiarize you with a typical integration project.
In this lab exercise, you will complete the following activities:
Use JBoss Developer Studio to create a new Fuse project.
View Apache Camel Routes.
Run a project locally.
Use the Palette within the Camel Editor to add a log processor.
1.1.1. Create a New Fuse Project
Launch JBoss Developer Studio on your computer.
Select another workspace
~/Temp/workspace-gpe-demo.Select File → New → Fuse Project.
Figure 1. JBoss Developer Studio - New Fuse ProjectDo not change the default project Location.
Click Next.
Figure 2. New Fuse Project - Select project locationIn the New Fuse Project — Select a project archetype to create and specify details window, select
camel-archetype-activemqas the Apache Maven archetype.For the Artifact Id select
demo1-camel.For the Version number, select
1.0.For the Package name, leave the default selection,
com.mycompany.demo1.camel.Figure 3. New Fuse Project - Select project archetype
Click Finish.
1.1.2. View the Apache Camel Routes
This project contains two Apache Camel routes. The first route consumes three XML files from the src/data directory and creates an Exchange for every XML file. The Exchange contains the file metadata as headers and properties, and it publishes a JMS Message to the personal.records queue.
The second route consumes the JMS Message from the queue, and it creates an Exchange that is evaluated against a condition using a Content Base Router (CBR)
Enterprise Integration Pattern (EIP). The CBR checks the JMS message and corresponding XML file to see if it contains the /person/city/ tag with a value equal to London. If the condition is matched, the Exchange creates a file with the contents of the JMS Message in the target/messages/uk directory.
If the condition is not matched, the Exchange creates a file in the target/messages/others directory instead.
Follow these steps to view the Apache Camel Routes:
On the menu, select Window → Open Perspective → Fuse Integration.
Figure 4. Open Perspective - Fuse IntegrationUse Project Explorer to open the
demo1-camelproject and verify that the contents of the project appear:src/main/javasrc/main/resources
Expand the collapsed
src/main/resourcesdirectory to reveal thespringsubfolder.In the
springsubfolder, double-click thecamel-context.xmlfile. The Fuse Camel Editor appears displaying an Apache Camel route.Figure 5. Camel IDE WindowOn the menu, select Routes, and then select a route to view, either Route: 1 or Route: 2.
Figure 6. Select a route to viewExplore the properties defined for an endpoint by selecting one in the Camel Editor view and reviewing the information in the Properties view:
Select the endpoint
activemq:personnel.records.Open the Properties view (if necessary) and inspect the endpoint URI, ID, and description.
In the Properties view, click the Documentation tab to review documentation for the endpoint/component.
Figure 7. Documentation tabReview the information for the
whenprocessor: The expression is/person/city = 'London'and the language isxpath.Verify that the URI syntax of the
activemqendpoint isactivemq:personnel.recordsClick the Source tab of the Camel Editor to inspect the Camel routes in XML format.
Both Java DSL and Camel Spring XML are supported languages for the source view. Figure 8. Sample XML file
Now you have seen what a typical Camel project looks like, and you have learned how to use the Camel Editor to view the Apache Camel Routes.
1.1.3. Run the Project Locally
A Fuse project is a collection of Camel Routes associated with a CamelContext. You start the Fuse project within JBoss Developer Studio. As explained in the course, whenever a Spring application context or XML module blueprint is created, the different beans declared within the camel-context.xml file are instantiated by Spring or OSGi Blueprint.
This is how both the DefaultCamelContext and the RouteBuilder classes (containing the DSL based Route definitions) are created.
Follow these steps to run the project:
Expand the folder
src/main/resourcesto reveal thespringfolder containing the filecamel-context.xml.Right-click the
camel-context.xmlfile and select Run as → Local Camel Context.Figure 9. Run Local Camel ContextThe Apache Camel Maven plug-in starts and the Maven console log shows that the CamelContext is created, routes are started, and endpoints are consumed.
Figure 10. Maven ConsoleRight-click the
targetdirectory of Project Explorer and select Refresh.Inspect the contents of the subdirectories
messages/ukormessages/othersto see the XML files that were created.Figure 11. Project Explorer - Target Directories
To differentiate between what is running within the JVM and what was created, use the JMX layer and the JMX Navigator to discover the different MBeans objects which form both the CamelContext as well as the ActiveMQ broker:
Select the JMX Navigator view.
Expand the Local Processes tree.
Click the green plus icon to add a new JMX Server connection.
Figure 12. JMX Server ConnectionIn the Create a new JMX Connection dialog box, click Next.
Click the Advanced tab and add the JMX Url:
service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmiClick Finish. The
CamelandBrokerJMX domains are now displayed with icons.Expand the JMX Server icon, and select the Camel domain.
Click Window→Show View→Other, and then select Diagram view to view a graphical representation of the Camel routes.
Switch to the Properties view to view the number and processing duration of the processed messages.
Figure 13. Sample JMX Navigator
1.1.4. Enable Tracing
Tracing enables you to track the contents of the Exchange and the activity of the processors.
Follow these steps to enable tracing:
If it is collapsed, expand the
CamelContextMbean within the JMX Navigator.Right-click
camel-1Mbean, and then select Start Tracing.Figure 14. JMX Navigator - Start TracingRefresh the
camel-1.Inspect the new icon (same icon as before but with a green bug) that represents the CamelContext.
Figure 15. JMX Navigator - Tracer ContextTo further test the tracing feature, create a new message and and use the Messages view and Properties view to review processing details:
Use Project Explorer to expand
src/data.Copy the contents of
message3.xmland paste into a new file,message4.xml.Save the new file in the same directory.
Figure 16. Sample - New MessageUse Project Explorer to expand
src/data.Copy the contents of
message3.xmland paste into a new filemessage5.xml.Save the new file in another directory, such as
src/testfor example, to excludemessage5.xmlfrom polling.Drag the message and drop it onto the
fileendpoint in the JMX Navigator.Figure 17. Sample - Add a File to an EndpointOpen the Properties view to inspect tabular information about the processed Exchanges, including:
Route ID
Processor ID
Exchanges Completed
Exchanges Failed
Mean Processing Time
Max Processing Time
Min Processing Time
Last Processing Time
Open the Messages view to inspect information reported by the trace feature about the content of the Exchange, including body and headers.
Figure 18. Sample - Tracing results
Click the red square icon in the Console view to terminate the Maven process.
Figure 19. Console - Terminate a Maven process
You have enabled and tested the tracing feature.
1.1.5. Add a Log Processor
In this activity, you explore the Palette in the Camel Editor and add a Log Processor to the project. The Palette contains various endpoints and processors that you use to design an Apache Camel project. They are organized by topic:
Endpoints
Routing
Control Flow
Transformation
Miscellaneous
Follow these steps to add a log processor:
From the Palette (right side of the Camel Editor view) select Log under the Components folder.
Drag the Log endpoint and drop it onto
Route 1.Figure 21. Sample - Add a Log EndpointInspect the Log endpoint icon that appears on the Design view.
Figure 22. Sample - Endpoint AddedIn the Design view, remove the connection between the
file:src/dataendpoint and theactivemqendpoint:Right-click the connection, and then select Remove.
Connect
file:src/datatolog.To remove any endpoint, you must first delete all connections to or from it.
In the Design view, add an arrow connecting the
log endpointto theactivemqendpoint:Move your cursor over the endpoint and click the Create Connection icon that appears.
Figure 23. Sample - Connecting Log Endpoint to Activemq
Edit the log endpoint to add a message
File has been processed.Figure 24. Sample - Activemq LogReview the message log.
Figure 25. Sample - Message LogRearrange the icons neatly and save the modified
camel-context.xmlfile.Figure 26. Sample - Exercise Result with IconsClick Run as → Local Camel Context to run the project again.
Verify that a message appears at least four times in the Maven console.
Figure 27. Sample - Results in Maven ConsoleIn Project Explorer, right-click the
demo1-camelproject and then select Close project.
1.2. Design a New Project
The goal of this exercise is to design a new integration project and create an Apache Camel Route using the Camel Editor. To do this, you use an existing Maven project and add to it a Camel route, a HelloBean, and business logic that sends a message to the console.
1.2.1. Import the Maven Project if not yet done
Open JBoss Developer Studio.
Import the
camel-labs-VERSION-NUMBER-excerciseApache Maven project:From the menu, Select File → Import.
Click Maven → Existing Maven Projects.
Figure 28. Import Maven Projects windowClick Finish.
In Project Explorer, confirm that the
camel-lab-2project was imported.Figure 29. Sample - Imported ProjectReview the project contents:
pom.xmlHelloBeanclass
1.2.2. Add the sayHello Method to the Bean
Before you design the Apache Camel Route, you must add a sayHello method to the body of the class HelloBean. This method will
be used by the route. The method accepts an exchange as an input parameter and returns a String.
Open the
HelloBeanclass and add asayHellomethod.public String sayHello(Exchange exchange) { String body = (String)exchange.getIn().getBody(); return "Hello world ! " + body; }
The return type and the object returned will be used by the Apache Camel engine to add a Body to the Exchange with the String object Hello world ! xxxx" |
1.2.3. Design the Apache Camel Route
You use the Camel Editor to create a new route that triggers a timer event at a specified interval (every X seconds). To design the route, you will complete these tasks:
Create a new CamelContext.
Edit the Endpoint and SetBody properties.
Add a bean tag and Bean processor to the project.
Add a log processor to the project.
Create a new CamelContext:
Open the
Fuse Integration perspective.In Project Explorer (left panel), expand the
src/main/resources/OSGI-INF/blueprintfolder.Right-click the
blueprint folderto select it.Select
New -> Camel XML File.Figure 30. Project Explorer - New Camel XML fileFor the Framework, select
OSGi Blueprint, and then click Finish.Inspect the folder to confirm that a new
camelContext.xmlwas added.
Edit the Endpoint and SetBody properties:
Double-click the
camelContext.xmlfile you just created to open it in the Camel Editor.Figure 31. Camel Route BlueprintFrom the Components section of the Palette, select the timer endpoint and drag it onto the project.
For the Uri field, select this text to define the URI:
timer://demo?delay=5000Figure 32. Timer EndpointFrom the Transformation section of the Palette, select the SetBody processor and drag it onto the project.
Edit the properties of this processor to add an expression and set the language:
Enter your name for the Expression.
For the Language, select
simple.Figure 33. SetBody Processor
Add the bean tag and bean processor:
Switch to the source of the Camel route to add the following
<bean>tag. This is required to instantiate the singleton bean, when the Blueprint context is created.<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:camel="http://camel.apache.org/schema/blueprint" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> <bean id="helloBean" class="com.redhat.gpe.training.camel.HelloBean"/>Return to the Design view.
From the Components section of the Palette, select a Bean processor and drag it onto the project.
Edit the properties of the Bean processor:
Enter
sayHellofor the Method.Select
helloBeanfor the the Bean Name.Figure 34. Bean Endpoint
Add a log processor:
From the Components section of the Palette, select a Log processor and drag it onto the project.
Edit the properties of this Log processor to add a simple expression that extracts the contents of the Exchange body:
For the Message, enter
${body}.Figure 35. Log Endpoint
Connect the endpoints and processors.
Figure 36. Route Blueprint - Connect ObjectsSave the modified Camel route and verify the result of the XML DSL route generated:
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:camel="http://camel.apache.org/schema/blueprint" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> <bean id="helloBean" class="com.redhat.gpe.training.camel.HelloBean"/> <camelContext trace="false" xmlns="http://camel.apache.org/schema/blueprint"> <route> <from uri="timer://demo?delay=5000" id="timer-of-the-demo"> <description/> </from> <setBody> <simple>Charles</simple> </setBody> <bean ref="helloBean" method="sayHello"/> <log message="${body}"/> </route> </camelContext> </blueprint>
1.2.4. Run the Project Locally
Right-click the
camel-context.xmlfile and select Run as → Local Camel Context.Verify that messages appear in the log.
Figure 37. Sample - Log Messages
You have completed this lab.